package com.vcc.web.controller;


import cn.dev33.satoken.annotation.SaCheckLogin;
import com.vcc.core.annotations.RestPack;
import com.vcc.data.entity.ClientPermission;
import com.vcc.web.core.util.UserUtil;
import com.vcc.service.client.vo.role.ClientPermissionVo;
import com.vcc.service.service.ClientPermissionService;
import jakarta.annotation.Resource;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.List;

@RestController
@RequestMapping("permission")
public class PermissionController {


    @Resource
    private ClientPermissionService clientPermissionService;


    @RestPack
    @SaCheckLogin
    @GetMapping("/list")
    public List<ClientPermissionVo> list() {
        List<ClientPermission> clientPermissions = clientPermissionService.list();
        //根据父子关系进行分组
        ArrayList<ClientPermissionVo> clientPermissionVos = new ArrayList<>();
        List<ClientPermission> parentId = clientPermissions.stream().filter(ClientPermission::getHasParent).toList();
        for (ClientPermission clientPermission : parentId) {
            ClientPermissionVo clientPermissionVo = new ClientPermissionVo(clientPermission);
            List<ClientPermissionVo> children = clientPermissions
                    .stream()
                    .filter(it -> it.getParentId().equals(clientPermission.getId()))
                    .toList()
                    .stream()
                    .map(ClientPermissionVo::new)
                    .toList();
            clientPermissionVo.setChildren(children);
            clientPermissionVos.add(clientPermissionVo);
        }
        return clientPermissionVos;
    }


    @RestPack
    @SaCheckLogin
    @GetMapping("/listAll")
    public List<String> listPermissionCode() {
        return clientPermissionService.listByUserId(UserUtil.getId());
    }

}
